package com.haixiaoke.saas.manage.menu.controller;

import com.haixiaoke.saas.common.annotation.Log;
import com.haixiaoke.saas.common.constant.UserConstants;
import com.haixiaoke.saas.common.core.domain.AjaxResult;
import com.haixiaoke.saas.common.enums.BusinessType;
import com.haixiaoke.saas.common.utils.StringUtils;
import com.haixiaoke.saas.menu.domain.BizMenu;
import com.haixiaoke.saas.menu.service.IBizMenuService;
import com.haixiaoke.saas.system.BaseController;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 企业端菜单权限Controller
 * 
 * @author panda
 * @date 2023-12-29
 */
@RestController
@RequestMapping("/manage/menu/menu")
public class ManageBizMenuController extends BaseController {
    @Autowired
    private IBizMenuService bizMenuService;

    /**
     * 查询企业端菜单权限列表
     */
    @PreAuthorize("@ss.hasPermi('business:menu:list')")
    @GetMapping("/list")
    public AjaxResult list(BizMenu bizMenu) {
        List<BizMenu> list = bizMenuService.selectBizMenuList(bizMenu);
        return success(list);
    }

    /**
     * 获取企业端菜单权限详细信息
     */
    @PreAuthorize("@ss.hasPermi('business:menu:query')")
    @GetMapping(value = "/{menuId}")
    public AjaxResult getInfo(@PathVariable("menuId") Long menuId) {
        return success(bizMenuService.selectBizMenuByMenuId(menuId));
    }

    /**
     * 获取菜单下拉树列表
     */
    @GetMapping("/treeselect")
    public AjaxResult treeselect(BizMenu bizMenu) {
//        bizMenu.setUserId(getUserId());
        return success(bizMenuService.buildMenuTreeSelect(bizMenu));
    }

    /**
     * 加载对应角色菜单列表树
     */
    @GetMapping(value = "/roleMenuTreeselect/{roleId}")
    public AjaxResult roleMenuTreeselect(@PathVariable("roleId") Long roleId) {
        BizMenu bizMenu = new BizMenu();
//        bizMenu.setUserId(getUserId());
        AjaxResult ajax = AjaxResult.success();
        ajax.put("checkedKeys", bizMenuService.selectMenuListByRoleId(roleId));
        ajax.put("menus", bizMenuService.buildMenuTreeSelect(bizMenu));
        return ajax;
    }

    /**
     * 新增企业端菜单权限
     */
    @PreAuthorize("@ss.hasPermi('business:menu:add')")
    @Log(title = "企业端菜单权限", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody BizMenu bizMenu) {
        if (!bizMenuService.checkMenuNameUnique(bizMenu)) {
            return error("新增菜单'" + bizMenu.getMenuName() + "'失败，菜单名称已存在");
        } else if (UserConstants.YES_FRAME.equals(bizMenu.getIsFrame()) && !StringUtils.ishttp(bizMenu.getPath())) {
            return error("新增菜单'" + bizMenu.getMenuName() + "'失败，地址必须以http(s)://开头");
        }
        bizMenu.setCreateBy(getUsername());
        return toAjax(bizMenuService.insertBizMenu(bizMenu));
    }

    /**
     * 修改企业端菜单权限
     */
    @PreAuthorize("@ss.hasPermi('business:menu:edit')")
    @Log(title = "企业端菜单权限", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody BizMenu bizMenu) {
        if (!bizMenuService.checkMenuNameUnique(bizMenu)) {
            return error("修改菜单'" + bizMenu.getMenuName() + "'失败，菜单名称已存在");
        } else if (UserConstants.YES_FRAME.equals(bizMenu.getIsFrame()) && !StringUtils.ishttp(bizMenu.getPath())) {
            return error("修改菜单'" + bizMenu.getMenuName() + "'失败，地址必须以http(s)://开头");
        } else if (bizMenu.getMenuId().equals(bizMenu.getParentId())) {
            return error("修改菜单'" + bizMenu.getMenuName() + "'失败，上级菜单不能选择自己");
        }
        bizMenu.setUpdateBy(getUsername());
        return toAjax(bizMenuService.updateBizMenu(bizMenu));
    }

    /**
     * 删除企业端菜单权限
     */
    @PreAuthorize("@ss.hasPermi('business:menu:remove')")
    @Log(title = "企业端菜单权限", businessType = BusinessType.DELETE)
	@DeleteMapping("/{menuId}")
    public AjaxResult remove(@PathVariable Long menuId) {
        if (bizMenuService.hasChildByMenuId(menuId)) {
            return warn("存在子菜单,不允许删除");
        }
        if (bizMenuService.checkMenuExistRole(menuId)) {
            return warn("菜单已分配,不允许删除");
        }
        return toAjax(bizMenuService.deleteBizMenuByMenuId(menuId));
    }
}
